<?php
if (!isset($_SESSION)){
	session_start();
	if(!isset($_SESSION['username'])){
		$_SESSION['username']='';
	}
}

if (!empty($_POST)){
	$username = trim($_POST['username']);
	$nickname = 'test';
	$pwd = md5($_POST['pwd']);
	$pwd2 = md5($_POST['pwd2']);
	$email = trim($_POST['email']);	
	$today=date("Y-m-d H:i:s",time());
    
    $pattern = "/^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/";
    
	if (empty($username)||
		empty($nickname)||
		empty($email)||
		empty($pwd)||
		empty($today)||
		$pwd2!= $pwd||
        !preg_match($pattern, $email)){
            exit('信息填写不完整!<a href="javascript:history.back(-1);">返回</a>');
	}

	require_once('lib/connbae.php');
	
	$sql = "INSERT INTO Author(username,nickname,pwd,email,pid,register_date) VALUES";
	$sql .= "('$username','$nickname','$pwd','$email','0','$today')";	

	$result =mysql_query($sql);		
	if (!$result){
		echo mysql_error().'<br/>';
		mysql_close();
		exit(' 操作失败, <a href="javascript:history.back(-1);">返回</a>');
	}  
	mysql_close();
	header("Refresh:5;Location: index.php");
	exit('注册成功 <a href="login.php?action=login">登录</a>');
}
?>
<!DOCTYPE HTML>
<html>
 <head>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<title>C Programing Learning</title>
	<link rel="stylesheet" type="text/css" href="css/base.css"/>
	<link rel="stylesheet" type="text/css" href="css/register.css"/>
	<link href="img/favour.ico" type="image/ico" rel="shortcut icon">
	<script type="text/javascript" src="#"></script>
<SCRIPT language="javascript"> 
<!-- 

--> 
</script>
</head>
<body>
	<div id="content">
		<div id="container">
				<div id="register">
					<form id="frmlogin" method="post" action="register.php">
						<p class="input">
							<label for="username" class="tblabel">用户姓名</label>
							<input type="text" aria-required="true" tabindex="1" size="12" value="" id="username" name="username" class="tbinput"/>
						</p><hr class="clear"/>
							
						<p class="input">
							<label for="pwd" class="tblabel">密　　码</label>
							<input type="password" aria-required="true" tabindex="2" size="12" value="" id="pwd" name="pwd" class="tbinput"/>
						</p><hr class="clear"/>
							
						<p class="input">
							<label for="pwd2" class="tblabel">确认密码</label>
							<input type="password" aria-required="true" tabindex="3" size="12" value="" id="pwd2" name="pwd2" class="tbinput"/>
						</p><hr class="clear"/>
							
						<p class="input">
							<label for="email" class="tblabel">邮　　箱</label>
							<input type="text" aria-required="true" tabindex="4" size="12" value="" id="email" name="email" class="tbinput"/>
						</p><hr class="clear"/>
							
						<p><input type="submit" value="提交" tabindex="5" id="submit" name="submit" class="submit"/></p><hr class="clear"/>
					</form>
				</div>
			</div>
		<div id="footer">
			<p>©2014 C语言在线答疑系统 v 1.0</p>
		</div>			
	</div>
</body>
</html>